package org.aceor.mddal.config;

import org.aceor.mddal.config.datasource.DsConfig;
import org.aceor.mddal.config.datasource.factory.DsConfigFactory;
import org.aceor.mddal.config.sharding.LogicTable;
import org.aceor.mddal.config.sharding.TableRule;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.text.MessageFormat;

/**
 * Created by lxue on 16/3/26.
 */
public class Main {

    @Test
    public void test_logictable(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
        LogicTable logicTable = applicationContext.getBean("logicTable", LogicTable.class);
        TableRule userTableRule = logicTable.getTableRules().get("user");
//        System.out.println(userTableRule.convertDbName("2"));
//        System.out.println(userTableRule.convertTbName("3"));
        System.out.println(logicTable.getDefaultDbIndex());
    }


    @Test
    public void test_format_replace() {
        String template = "A_B_C_{0}_E";

        System.out.println(new MessageFormat(template).format(new String[]{"18987"}));
    }

    @Test
    public void test_config_parse() throws Exception {
//        XStream xStream = new XStream();
//        xStream.processAnnotations(new Class[]{DsConfig.class, GroupNode.class, AtomNode.class, Atom.class});
//        DsConfig dsConfig = (DsConfig) xStream.fromXML(Main.class.getResourceAsStream("/datasource/mddal-ds.xml"));

        DsConfigFactory.buildDsConfig("/datasource/mddal-ds.xml", new DsConfigFactory.DsConfigChangeListener() {
            @Override
            public void onChange(DsConfig dsConfig) {

            }
        });
    }
}
